package server.model.meeting;

import java.io.Serializable;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Vector;

import client.remotes.ICommunicator;
import client.whiteboard.model.DrawingData;

import server.model.remotes.IMeeting;

public class Meeting extends UnicastRemoteObject implements IMeeting, Serializable {
	
	private ArrayList<Participant> participants;
	private Logbook logbook;
	private WhiteboardData whiteboardData;
	private MeetingInfo meetingInfo;
	private ArrayList<ICommunicator> communicators;
	
	public Meeting() throws RemoteException {
		super();
		
		communicators = new ArrayList<ICommunicator>();
		participants = new ArrayList<Participant>();
		whiteboardData = new WhiteboardData();
		meetingInfo = new MeetingInfo();
	}
	public void distributeMessage(Message message)throws RemoteException{	
		logbook.addMessage(message);
		Iterator<ICommunicator> ci = communicators.iterator();

		while (ci.hasNext()){
			ICommunicator currentCom =  ci.next();
			currentCom.SendMessage(message);
		}
	}

	public void distributeWhiteboard(String username, DrawingData drawingData)throws RemoteException{	
		Iterator<ICommunicator> ci = communicators.iterator();
		while (ci.hasNext()){
			ICommunicator currentCom =  ci.next();
			currentCom.SendWhiteboard(username, drawingData);
		}
	}

	public int getWhiteboardAccess()throws RemoteException{
		return 0;		
	}

	public Logbook getLogbook()throws RemoteException{
		return logbook;		
	}

	public void addParticipant(Participant participant) {
		participants.add(participant);
	}

	public ArrayList<Participant> getParticipants() {
		return participants;
	}
	
	public void setLogbook(Meeting _meeting) {
		this.logbook = new Logbook(_meeting);
	}

	public void setWhiteboard(WhiteboardData whiteboard) {
		this.whiteboardData = whiteboard;
	}

	public WhiteboardData getWhiteboard(){ 
		return whiteboardData;
	}

	public void setMeetingInfo(MeetingInfo meetingInfo) {
		this.meetingInfo = meetingInfo;
		logbook = new Logbook(this);
	}

	public MeetingInfo getMeetingInfo() {
		return meetingInfo;
	}
	@Override
	public void addCommunicator(ICommunicator communicator)throws RemoteException {
		communicators.add(communicator);
		Iterator<ICommunicator> ci = communicators.iterator();
		while (ci.hasNext()){
			ICommunicator currentCom =  ci.next();
			currentCom.updateParticipants(getClients());
		}
	}
	private ArrayList getClients()throws RemoteException
	{
		ArrayList<String> clients = new ArrayList();
		for(int i = 0; i < communicators.size(); i++){
			String client = communicators.get(i).getName();
			clients.add(client);
		}
		return clients;
	}
}
